package com.ys.everydaynews.utils

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.listener.OnImageCompleteCallback
import com.luck.picture.lib.widget.longimage.SubsamplingScaleImageView
import android.graphics.drawable.Drawable

import android.graphics.PointF

import com.luck.picture.lib.widget.longimage.ImageViewState

import com.luck.picture.lib.widget.longimage.ImageSource

import android.view.View

import com.luck.picture.lib.tools.MediaUtils

import android.graphics.Bitmap

import com.bumptech.glide.request.target.CustomTarget
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory

import com.bumptech.glide.request.target.BitmapImageViewTarget

import com.ys.everydaynews.R


object GlideEngine : ImageEngine {


    override fun loadImage(context: Context, url: String, imageView: ImageView) {
        if (!ImageLoaderUtils.assertValidRequest(context)) {
            return;
        }
        Glide.with(context)
            .load(url)
            .into(imageView);
    }

    override fun loadImage(
        context: Context,
        url: String,
        imageView: ImageView,
        longImageView: SubsamplingScaleImageView?,
        callback: OnImageCompleteCallback?
    ) {
        if (!ImageLoaderUtils.assertValidRequest(context)) {
            return
        }
        Glide.with(context)
            .asBitmap()
            .load(url)
            .into(object : CustomTarget<Bitmap?>() {
                override fun onLoadStarted( placeholder: Drawable?) {
                    super.onLoadStarted(placeholder)
                    callback?.onShowLoading()
                }

                override fun onLoadFailed( errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    callback?.onHideLoading()
                }

                override fun onResourceReady(
                    resource: Bitmap,
                    transition: com.bumptech.glide.request.transition.Transition<in Bitmap?>?
                ) {
                    callback?.onHideLoading()
                    val eqLongImage = MediaUtils.isLongImg(
                        resource.width,
                        resource.height
                    )
                    longImageView!!.visibility = if (eqLongImage) View.VISIBLE else View.GONE
                    imageView.visibility = if (eqLongImage) View.GONE else View.VISIBLE
                    if (eqLongImage) {
                        // 加载长图
                        longImageView!!.isQuickScaleEnabled = true
                        longImageView!!.isZoomEnabled = true
                        longImageView!!.setDoubleTapZoomDuration(100)
                        longImageView!!.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
                        longImageView!!.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER)
                        longImageView!!.setImage(
                            ImageSource.cachedBitmap(resource),
                            ImageViewState(0F, PointF(0F, 0F), 0)
                        )
                    } else {
                        // 普通图片
                        imageView.setImageBitmap(resource)
                    }
                }

                override fun onLoadCleared(placeholder: Drawable?) {}
            })
    }

    override fun loadFolderImage(context: Context, url: String, imageView: ImageView) {
        if (!ImageLoaderUtils.assertValidRequest(context)) {
            return
        }
        Glide.with(context)
            .asBitmap()
            .load(url)
            .override(180, 180)
            .centerCrop()
            .sizeMultiplier(0.5f)
            .placeholder(R.drawable.picture_image_placeholder)
            .into(object : BitmapImageViewTarget(imageView) {
                override fun setResource(resource: Bitmap?) {
                    val circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.resources, resource)
                    circularBitmapDrawable.cornerRadius = 8f
                    imageView.setImageDrawable(circularBitmapDrawable)
                }
            })
    }

    override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
        if (!ImageLoaderUtils.assertValidRequest(context)) {
            return;
        }
        Glide.with(context)
            .load(url)
            .override(200, 200)
            .centerCrop()
            .placeholder(R.drawable.picture_image_placeholder)
            .into(imageView);
    }

    private fun GlideEngine() {}

    private var instance: GlideEngine? = null

    fun createGlideEngine(): GlideEngine? {
        if (null == instance) {
            synchronized(GlideEngine::class.java) {
                if (null == instance) {
                    instance = GlideEngine
                }
            }
        }
        return instance
    }
}